use scopeguard::defer;

use bc_base::{
    BlockId, TransactionId,
    constants::{COINBASE_TX_ID_OF_GENESIS_BLOCK, GENESIS_BLOCK_ID},
    wallet::ItemNumberInWalletDb,
};

use tests::common;

#[tokio::test]
async fn check_init_state() {
    let (test_env, tmp_dir) = common::setup("check_init_state", false, None, false).await;
    defer! {
        tmp_dir.close().unwrap();
    };

    // do test here
    {
        let block_id = BlockId::from_hex_str(GENESIS_BLOCK_ID).unwrap();
        let tx_id = TransactionId::from_hex_str(COINBASE_TX_ID_OF_GENESIS_BLOCK).unwrap();
        let bc_manager = test_env.bc_manager.lock().await;
        assert_eq!(bc_manager.block_number().await, 1);
        assert_eq!(bc_manager.transaction_number().await, 1);
        assert!(bc_manager.block_exists(&block_id).await);
        assert!(bc_manager.transaction_exists(&tx_id).await);

        let prev_block_id = bc_manager.get_prev_block_id(&block_id).await.unwrap();
        assert_eq!(prev_block_id, BlockId::new_null());

        let wallet_item_number = ItemNumberInWalletDb::new();
        assert_eq!(bc_manager.wallet_item_number().await, wallet_item_number);
        drop(bc_manager);

        common::local::prepare_wallet_for_genesis(test_env.bc_manager.clone()).await;

        let wallet_item_number = ItemNumberInWalletDb {
            account_key_pair_number: 1,
            account_name_number: 0,
            default_key_number: 0,
            wallet_tx_number: 1,
        };
        let bc_manager = test_env.bc_manager.lock().await;
        assert_eq!(bc_manager.wallet_item_number().await, wallet_item_number);
    }

    common::teardown(test_env).await;
}
